/*
#Copyright (c) 2013, Philippe Bordron <philippe.bordron@gmail.com>
#
# This file is part of SIPPER.
#
# DCC is free software: you can redistribute it and/or modify
# it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# DCC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU LESSER GENERAL PUBLIC LICENSE for more details.
#
# You should have received a copy of the GNU LESSER GENERAL PUBLIC LICENSE
# along with SIPPER.  If not, see <http://www.gnu.org/licenses/>
*/
package combi.wip.sipper.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map.Entry;

public class Profil extends HashMap<String, String>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/*
	 *  une ligne de profil est ecrite sous cette forme:
	 *  id = (relative)path_to-file
	 */
	public static Profil createProfil(String fileIn) throws IOException
	{
		Profil result = new Profil();
		BufferedReader in = new BufferedReader(new FileReader(fileIn));
		String ch;
		while ((ch = in.readLine()) != null)
		{
			if (!ch.startsWith("#") && !ch.isEmpty())
			{
				String[] keyValue = ch.split("\\s*=\\s*");
				if(keyValue[1].startsWith("~/"))
				{
					keyValue[1] = System.getProperty("user.home") + "/" + keyValue[1].substring(2);
				}
				result.put(keyValue[0], keyValue[1]);
			}
		}
		in.close();
		return result;
	}
	
	public void writeProfil (String fileOut) throws IOException
	{
		BufferedWriter out = new BufferedWriter(new FileWriter(fileOut));
		for (Entry<String, String> entry : this.entrySet())
		{
			out.write(entry.getKey() + " = " + entry.getValue() + "\n");
		}
		out.close();
	}
	
	
/*	//vérifie un profil en fonction d'un modele
	public static boolean isValidProfil()
	{
		//TODO
		return true;
	}*/
	
}
